package com.longcai.piaojia.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.longcai.piaojia.BaseApplication;
import com.longcai.piaojia.R;
import com.longcai.piaojia.conn.UpdateAddressPost;
import com.longcai.piaojia.conn.UserAddressShowGet;
import com.longcai.piaojia.dialog.ChooseAreaDialog;
import com.longcai.piaojia.dialog.WarnningDialog;
import com.longcai.piaojia.fragment.MineFragment;
import com.longcai.piaojia.util.MyToast;
import com.longcai.piaojia.util.Validator;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilMatches;
import com.zcx.helper.util.UtilToast;

/**
 * Created by Administrator on 2016/9/2.
 */
public class MyReceiveAddressEditActivity extends BaseActivity implements View.OnClickListener {

    @BoundView(R.id.myreceive_address_edit_name_et) private EditText myreceive_address_edit_name_et;
    @BoundView(R.id.myreceive_address_edit_phonenum_et) private EditText myreceive_address_edit_phonenum_et;
    @BoundView(R.id.myreceive_address_edit_area_tv) private TextView myreceive_address_edit_area_tv;
    @BoundView(R.id.myreceive_address_edit_address_et) private EditText myreceive_address_edit_address_et;
    @BoundView(R.id.myreceive_address_edit_idcard_et) private EditText myreceive_address_edit_idcard_et;
    @BoundView(R.id.myreceive_address_edit_saveaddress_tv) private TextView myreceive_address_edit_saveaddress_tv;

    private String mAreaId;
    private UserAddressShowGet.UserAddressEntity userAddressEntity;
    private boolean isChoose = false;

    private UpdateAddressPost updateAddressPost = new UpdateAddressPost(new AsyCallBack<String>(){
        @Override
        public void onSuccess(String toast, int type, String s) throws Exception {

            UtilToast.show(MyReceiveAddressEditActivity.this, toast);

            ((MyReceiveAddressActivity.refreshAddressCallback) getAppCallBack(MyReceiveAddressActivity.class)).refreshAddress();

            if(isChoose){

                if(userAddressEntity.status.equals("1")){

                    try {
                        DispatchingTypeActivity.RefreshDefaultAddress refreshDefaultAddress = (DispatchingTypeActivity.RefreshDefaultAddress) getAppCallBack(DispatchingTypeActivity.class);
                        refreshDefaultAddress.RefreshAddress(1);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }

            }

            finish();

        }

        @Override
        public void onFail(String toast, int type) throws Exception {

           UtilToast.show(MyReceiveAddressEditActivity.this, toast);

        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_myreceive_address_edit);

        setBackTrue();

        setTitleName(getResources().getString(R.string.myreceive_address_edit_saveaddress_text));

        userAddressEntity = (UserAddressShowGet.UserAddressEntity) getIntent().getExtras().getSerializable("UserAddressEntity");

        isChoose = getIntent().getExtras().getBoolean("isChoose", false);

        myreceive_address_edit_name_et.setText(userAddressEntity.name);
        myreceive_address_edit_phonenum_et.setText(userAddressEntity.phone);
        myreceive_address_edit_area_tv.setText(userAddressEntity.city);
        myreceive_address_edit_address_et.setText(userAddressEntity.address);
        myreceive_address_edit_idcard_et.setText(userAddressEntity.idnumber);

        mAreaId = userAddressEntity.region_id;

        myreceive_address_edit_saveaddress_tv.setOnClickListener(this);
        myreceive_address_edit_area_tv.setOnClickListener(this);

    }


    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.myreceive_address_edit_area_tv:

                new ChooseAreaDialog(this) {
                    @Override
                    public void onConfirm(String province, String provinceId, String city, String cityId, String area, String areaId) {

                        if (TextUtils.isEmpty(areaId)) {

                            myreceive_address_edit_area_tv.setText(province + getResources().getString(R.string.space) + city);
                            mAreaId = cityId;
                        }else{

                            myreceive_address_edit_area_tv.setText(province + getResources().getString(R.string.space) + city + getResources().getString(R.string.space) + area);
                            mAreaId = areaId;
                        }

                    }
                }.show();

                break;

            case R.id.myreceive_address_edit_saveaddress_tv:

                String nameStr = myreceive_address_edit_name_et.getText().toString().trim().replaceAll(" ", "");
                String phoneStr = myreceive_address_edit_phonenum_et.getText().toString().trim().replaceAll(" ", "");
                String addressStr = myreceive_address_edit_address_et.getText().toString().trim().replaceAll(" ", "");
                String idcardStr = myreceive_address_edit_idcard_et.getText().toString().trim().replaceAll(" ", "");

                if (TextUtils.isEmpty(nameStr)) {

                    MyToast.show(context, getString(R.string.myreceive_address_add_name_hint), Toast.LENGTH_LONG);

                    return;
                } else if (TextUtils.isEmpty(phoneStr)) {

                    MyToast.show(context, getString(R.string.myreceive_address_add_phonenum_hint), Toast.LENGTH_LONG);

                    return;
                } else if (!UtilMatches.checkMobile(phoneStr)) {

                    MyToast.show(context, getString(R.string.warning_please_fill_true_phone_text), Toast.LENGTH_LONG);

                    return;
                } else if (TextUtils.isEmpty(mAreaId)) {

                    MyToast.show(context, getString(R.string.warning_please_receive_address_area_text), Toast.LENGTH_LONG);

                    return;
                } else if (TextUtils.isEmpty(addressStr)) {

                    MyToast.show(context, getString(R.string.myreceive_address_add_address_hint), Toast.LENGTH_LONG);

                    return;
                }

                if (!TextUtils.isEmpty(idcardStr) &&  !Validator.isIDCard(idcardStr)) {

                    MyToast.show(context, getString(R.string.warning_please_fill_true_idnum_text), Toast.LENGTH_LONG);

                    return;
                }


                updateAddressPost.id = userAddressEntity.id;
                updateAddressPost.name = nameStr;
                updateAddressPost.phone = phoneStr;
                updateAddressPost.address = addressStr;
                updateAddressPost.region_id = mAreaId;
                updateAddressPost.idnumber = idcardStr;
                updateAddressPost.user_id = BaseApplication.BasePreferences.readUserId();

                updateAddressPost.execute(MyReceiveAddressEditActivity.this);

                break;

        }

    }



}
